package com.cb.controller.admin.system;


import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.cb.common.util.EncryptUtil;
import com.cb.core.controller.BaseFormController;
import com.cb.domain.model.system.SystemUser;
import com.cb.domain.service.system.SystemUserManager;

/**
 * 
 * @Author author name
 * Create Time:
 * <p>Class Description Here [TODO]</p>
 */
public class SystemUserFormController extends BaseFormController {
	
	@Autowired
	private SystemUserManager systemUserManager=null;
	
	public SystemUserFormController() {
		 setCommandName("systemUser");
	     setCommandClass(SystemUser.class);
	}
	
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		
		SystemUser systemUser=(SystemUser)command;
		String msg=null;
		String password= systemUser.getPassword();
		password=EncryptUtil.md5Translate(password);
		systemUser.setPassword(password);
		//validate the logic business data here [TODO]
		if(systemUser.getSystemUserId()==null){
			msg=getText("systemUser.added",systemUser.getSystemUserId());
			systemUser.setCreateTime(new Date());
		}else{
			msg=getText("systemUser.updated",systemUser.getSystemUserId());
		}
		
		systemUserManager.save(systemUser);
		
		saveMessage(request,msg);
		
		return new ModelAndView(getSuccessView());
	}
	
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		SystemUser systemUser=null;
		String systemUserId=request.getParameter("systemUserId");
		if(StringUtils.isNotEmpty(systemUserId)){
			systemUser=systemUserManager.get(new Long(systemUserId));
		}else{
			systemUser=new SystemUser();
		}
		return systemUser;
	}
	
}
